import pandas as pd
import numpy as np

# arr =np.random.randint(0,10,size=5)
# s1 = pd.Series(arr)
#
# s2 =  pd.Series(data=arr,index=['a','b','c','d','e'])
#
# l1 = [1,2,3,4]
# s3 = pd.Series(l1,index=list('ABCD'))

# user_info = {
#     'name': 'anan',
#     'age': 20,
#     'address': 'AAA'
# }
#
# s4 = pd.Series(user_info,index=['name','address'])


# index = list('ABCDE')
# data =[6,6,6,6,6]
#
# s5 = pd.Series(6,index)
# print(s5)
# s6 = pd.DataFrame(pd.Series(data=[1,2,3],index=list('abc'),name='kk'))
# s6 = pd.Series(data=[1,2,3],index=list('abc'),name='kk')
# print(s6)
# print(s6.shape)
# print(s6.size)
# print(s6.index)
# print(s6.values)
# s = pd.Series(data=[1,2,3],index=list('abc'),name='kk')
# arr = np.array([1,2,3])
# print(s + arr)
# print(np.power(s,3))
# s1 = pd.Series(data=np.array([2,3,4]),index=['c','d','a'])
# print(s1+s)

#26 DateFrame

# list1 = [1, 2, 'name']
# s1 = pd.Series(list1)
#
# print(s1)
# data = np.random.randint(0,100,size=(3,4))
# index = ['tom','lucy','alice']
# columns =['语文','数学','物理','化学']
# df = pd.DataFrame(data=data,index=index,columns=columns)
#
# print(df)

# name = ['tom','lucy','alice','xixi','hh']
# yuwen = np.random.randint(0,100,size=5)
# shuxue = np.random.randint(0,100,size=5)
#
# dictionary = {
#     'name':name,
#     'yuwen':yuwen,
#     'shuxue':shuxue
# }
#
# df = pd.DataFrame(data=dictionary)
# print(df)

#2.8 DateFrame
# name = pd.Series(data=['tom','lucy','alice','xixi','hh'],index=list('abcde'))
# scores = pd.Series(data=np.random.randint(0,100,size=5),index=list('abcde'))
# dic = {
#     'name':name,
#     'scores':scores
# }
# df = pd.DataFrame(data={
#     'name':name,
#     'scores':scores
# })

# dic = {
#     'name':'ll',
#     'scores':90
# }
# dic1 = {
#     'name':'yy',
#     'scores':80
# }
# dic2 = {
#     'name':'kk',
#     'scores':60
# }
# ll = [dic, dic1, dic2]
# df2 = pd.DataFrame(data=ll, index=list('abc'))
# print(df2)
#2.9 DateFrame
# df = pd.DataFrame(data=np.random.randint(0, 10,size=(3, 4)), columns=list('ABCD'))
# print(df)
# print(df + 10)
# arr =  np.array([1,2,3,4])
# arr2 = arr.reshape((4,1)).copy()
# print(df + arr)
# se = pd.Series(data=[1,2,3,4],index=list('ABCD'))
# print(df + se)
# se2 = pd.Series(data=[1,2,3],index=[0,1,2])
# print(df.add(se2,axis="index") )


# df1 = pd.DataFrame(data=np.random.randint(0, 10,size=(3, 3)), columns=list('ABC'))
# df2 = pd.DataFrame(data=np.random.randint(0, 10,size=(2, 2)), columns=list('BA'))
# print(df1)
# print(df2)
# print(df1.add(df2,fill_value=0))

# arr = np.random.randint(-10, 10,size=(3, 3))
# df = pd.DataFrame(data=arr)
# print(df)
# # print(np.abs(df))
#
# print(df.T)
# arr = np.random.randint(0, 10, size=(5, 6))
# # print(arr[:,0:1])
#
# df = pd.DataFrame(data=arr,columns=list('abcdef'))
# print(df)
# print(df.values)
# print(df.loc[:,'a':'e'])
# print(df.loc[[0,1],['c','b']])
# print(df.loc[[True,True,False,False,False]])

# bool_list = pd.Series(data=np.array([True,True,False,False,True,False]),index=[0,3,2,1,4,5])
# print(df.loc[bool_list])
# print(bool_list)
# print(bool_list.values)
# print(df.loc[:,bool_list.values])
# print(df.iloc[:,0])
# print(df.iloc[[True,True,False,False,True]])
# print(df[['a','b']])

# df = pd.DataFrame(data=np.random.randint(0,100,size=(5,4)),columns=list('abcd'))

# print(df.where(df > 60,other='不及格'))
# print(df.where(cond=df['a'] > 60 ,other='其他'))
# print(df.where(df > 60,other=pd.Series(data=[100,200],index=['a','c']),axis=1))
# print(df.where(df > 60,other=pd.Series(data=[100,200],index=[1,2]),axis=0))
# print(df.mask(cond=df > 60,other=pd.Series(data=[100,200],index=['a','c']),axis=1))
# print(df.query('a > 60 and b > 60'))
# df.columns = ['aa','bb','cb','db']
# print(df.filter(items=['aa','bb']))
# print(df.filter(regex='^b',axis=1))

# arr = df.values
# print(arr.sum(axis=1))
# print(arr.mean(axis=0))
# arr = arr.astype(np.float32)

# df.loc[1,'b'] = np.nan
# print(df.sum(axis=1))
# print(df.sum().sum())


# index = ['张三', '李四', '赵五', '王六']
# columns = ['python', 'java', 'C']
# score1 = pd.DataFrame(data=np.random.randint(40,100,size=(4, 3)), index=index, columns=columns)
# score2 = pd.DataFrame(data=np.random.randint(40,100,size=(4, 3)), index=index, columns=columns)
# print(score1)
# # print((score1+score2)/2)
# score1.loc['张三', 'java'] = 0
# score1.loc['李四'] += 10
# print(score1)
#
# # print(score1.query('python > java'))
# conf = score1['python'] > score1['java']
# print(score1.loc[conf].index)


# df = pd.DataFrame(data=np.random.randint(0,10,size=(5,3)),columns=list('abc'))
# m_index = pd.RangeIndex(start=0,stop=10,step=2,name='kk')
# df.index=m_index
# print(df)
# print(df.index)
# print(df.columns)
#2.20

# level1 = ['第一期','第二期']
# level2 = ['A','B','C']

# columns = pd.MultiIndex.from_product([level1,level2],names=['期数','产品'])
# index = pd.Index(data=['lucty','tom','alex'],name='销售员')
#
# data = pd.DataFrame(data=np.random.randint(0,100,size=(3,6)),index=index,columns=columns)
# print(data)

# index = pd.MultiIndex.from_product([['第一期','第二期'], ['lucy', 'alex', 'tom']],names=['期数','销售员'])
# colunms = pd.Index(data=['A','B','C'],name='产品名称')
# data = pd.DataFrame(data=np.random.randint(0,100,size=(6,3)),index=index,columns=colunms)
# # print(data.loc[[('第一期','alex'),('第二期','alex')]])
# data = data.sort_index()
# # k = data.loc[('第一期','alex'):('第二期','lucy')]
# # print(k)
#
# print(data.unstack(level=1).unstack())
# # print(data.stack())

# level1 = ['期中','期末']
# level2 = ['python','java','C']
# columns = pd.MultiIndex.from_product([level1,level2],names=['期数','学科'])
# index = pd.Index(data=['lucy','tom','jack'],name='学生')
# data = pd.DataFrame(data=np.random.randint(30,100,size=(3,6)),index=index,columns=columns)
# data = data.sort_index()
# print(data)
# print(data.loc['lucy',data.loc['lucy'] == data.loc['lucy'].max()].index[0])
# tom_score = data.loc['tom'].unstack()
# print(tom_score.mean())
# data.loc['jack',('期中','python')] += 20
# print(data)
# data1 = data.stack(level=-2).unstack(level=-2).stack(level=-1)
# print(data1)
# print(data1.loc[[('期中','tom'),('期末','tom')]].mean())


# df = pd.read_csv('../datafile/csv文件.csv',sep=',',header=None,index_col=0)
df = pd.read_table('../datafile/txt文件.txt',header=None,sep='\s+',index_col=0)
print(df)
#2.25
